package com.mdc.mansion.web.modules.system.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mdc.mansion.common.util.StringUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.Menu;
import com.mdc.mansion.web.common.web.BaseController;
import com.mdc.mansion.web.modules.system.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 菜单Controller
 * @author chenmingdong
 * @version 2016-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/system/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public Menu get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getMenu(id);
		}else{
			return new Menu();
		}
	}
	
	@RequiresPermissions("system:menu:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(Menu menu) {
		Map<String, Object> model=new HashMap<String, Object>();
		List<Menu> list = Lists.newArrayList();
		List<Menu> sourcelist = systemService.findAllMenu();
		Menu.sortList(list, sourcelist,"1",true);
		model.put("list", list);
		ModelAndView modelAndView=new ModelAndView("modules/system/menuList",model);
		return modelAndView;
	}
	
	@RequiresPermissions("system:menu:view")
	@RequestMapping(value = "form")
	public ModelAndView form(Menu menu) {
		Map<String, Object> model=new HashMap<String, Object>();
		if(StringUtils.isBlank(menu.getId()) && StringUtils.isNotBlank(menu.getParentId())){
			Menu menu2 = systemService.getMenu(menu.getParentId());
			menu.setParent(menu2);
		}
		model.put("menu", menu);
		return new ModelAndView("modules/system/menuForm",model);
	}
	
	/**
	 * 批量修改菜单排序
	 */
	@RequiresPermissions("system:menu:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
    	for (int i = 0; i < ids.length; i++) {
    		Menu menu = new Menu();
    		menu.setSort(sorts[i]);
    		menu.setId(ids[i]);
    		systemService.updateMenuSort(menu);
    	}
    	addMessage(redirectAttributes, "保存菜单排序成功!");
		return "redirect:" + adminPath + "/system/menu/";
	}
	

	@RequiresPermissions("system:menu:edit")
	@RequestMapping(value = "save")
	public ModelAndView edit(Menu menu,RedirectAttributes redirectAttributes) {
//		cmsMenuService.save(menu);
		systemService.saveMenu(menu);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("system:menu:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(Menu menu,RedirectAttributes redirectAttributes) {
		systemService.deleteMenu(menu);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	/**
	 * isShowHide是否显示隐藏菜单
	 * @param extId
	 * @param isShowHidden
	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId,@RequestParam(required=false) String isShowHide) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Menu> list = systemService.findAllMenu();
		for (int i=0; i<list.size(); i++){
			Menu e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				if(isShowHide != null && isShowHide.equals("0") && e.getIsShow().equals("0")){
					continue;
				}
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}
